import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ost_flutter_module/class/store/city_page/store_city_page.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'dart:ui';
import 'package:ost_flutter_module/common/color.dart';
import 'package:ost_flutter_module/common/utils.dart';
import 'package:ost_flutter_module/help/ost_route.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_size_icon_button.dart';
import 'package:ost_flutter_module/widget/custom_widget/image_text_button.dart';
import 'package:flutter_boost/boost_navigator.dart';

class StoreAppBar extends StatefulWidget {
  StoreAppBar({Key key}) : super(key: key);

  @override
  _StoreAppBarState createState() => _StoreAppBarState();
}

class _StoreAppBarState extends State<StoreAppBar> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        StoreAppBarLeading(),
        SizedBox(width: Adapt.px(8)),
        Expanded(child: StoreAppBarSearchBar()),
        SizedBox(width: Adapt.px(8)),
        GIconButton(
          svg: Utils.svgAssets('shop_cart'),
          color: colorFACD33,
          onPressed: () {
            Navigator.pushNamed(context, GRouter.shop_cart);
            // BoostNavigator.of().push(name);
          },
        ),
      ],
    );
  }
}

class StoreAppBarLeading extends StatefulWidget {
  const StoreAppBarLeading({Key key}) : super(key: key);

  @override
  _StoreAppBarLeadingState createState() => _StoreAppBarLeadingState();
}

class _StoreAppBarLeadingState extends State<StoreAppBarLeading> {
  String localName;

  @override
  void initState() {
    super.initState();
    var cityModel =
        SpUtil.getObj<CityModel>(SpUtilKey.city, (v) => CityModel.fromJson(v));
    if (cityModel == null)
      localName = '深圳';
    else
      localName = cityModel.city;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GImageTextButton(
        localName,
        backgroundColor: Colors.transparent,
        svg: Utils.svgAssets('icon_location'),
        textLeadingPadding: 2,
        onTap: (value) async {
          var model = await Navigator.pushNamed(
            context,
            GRouter.city_page,
            arguments: CityModel(city: localName),
          );

          if (model == null) return;

          // 缓存 已选城市
          if (model is CityModel) {
            SpUtil.putObject(SpUtilKey.city, model);
            localName = model.city;
            setState(() {});
          }
        },
      ),
    );
  }
}

class StoreAppBarSearchBar extends StatelessWidget {
  const StoreAppBarSearchBar({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: primaryColor,
      shape: StadiumBorder(),
      child: Ink(
        decoration:
            BoxDecoration(borderRadius: BorderRadius.circular(Adapt.px(15))),
        child: InkWell(
          borderRadius: BorderRadius.circular(Adapt.px(15)),
          onTap: () {
            Navigator.pushNamed(context, GRouter.course_search_page);
          },
          child: SizedBox(
            height: Adapt.px(30),
            child: Row(
              children: [
                SizedBox(width: Adapt.px(8)),
                Icon(Icons.search, color: color999999),
                SizedBox(width: Adapt.px(8)),
                Text(
                  '搜索',
                  style: TextStyle(color: color999999, fontSize: 14),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
